package com.care.modules.alarm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.care.common.device.vo.function.InstanceFunctionVo;
import lombok.extern.slf4j.Slf4j;
import com.care.modules.alarm.entity.ActuatorInvokeFunction;
import com.care.modules.alarm.mapper.ActuatorInvokeFunctionMapper;
import com.care.modules.alarm.service.IActuatorFunctionService;
import com.care.modules.device.entity.DeviceInstance;
import com.care.modules.device.service.IDeviceFunctionService;
import com.care.modules.device.service.IDeviceInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: forallcn-iotps
 * @description: IActuatorFunctionService
 * @author: zhouwr
 * @create: 2021-03-05 09:55
 * @version：1.0
 **/
@Service
@Slf4j
public class ActuatorFunctionServiceImpl extends ServiceImpl<ActuatorInvokeFunctionMapper, ActuatorInvokeFunction> implements IActuatorFunctionService {
    @Autowired
    private IDeviceFunctionService functionService;
    @Autowired
    private IDeviceInstanceService instanceService;

    @Override
    public InstanceFunctionVo getExecuteConfig(String actuatorId) {
        ActuatorInvokeFunction invokeFunction = this.getById(actuatorId);
        log.debug("invokeFunction >>> {}", invokeFunction);
        InstanceFunctionVo functionVo = new InstanceFunctionVo();
        DeviceInstance instance = instanceService.getById(invokeFunction.getInstanceId());
        assert instance != null;
        try {
            functionVo = instanceService.buildFunctionStructures(instance.getId()).stream()
                    .filter(function -> function.getId().equals(invokeFunction.getFunctionId()))
                    .findAny().get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return functionVo;
    }
}
